import { Request } from 'express';

// 定义浏览器信息接口
export interface UserAgentInfo {
  browser: {
    name: string;
    version: string;
  };
  os: {
    name: string;
    version: string;
  };
  device: string;
  isMobile: boolean;
}

// 保存请求对象，通过中间件注入
let currentRequest: Request | null = null;

/**
 * 设置当前请求对象
 */
export function setCurrentRequest(req: Request): void {
  currentRequest = req;
}

/**
 * 获取用户代理信息
 * 简化版实现，生产环境可考虑使用更完整的ua-parser-js等库
 */
export function getUserAgent(): UserAgentInfo {
  const defaultInfo: UserAgentInfo = {
    browser: { name: 'Unknown', version: '' },
    os: { name: 'Unknown', version: '' },
    device: 'Unknown',
    isMobile: false
  };

  if (!currentRequest) {
    return defaultInfo;
  }

  const userAgentString = currentRequest.headers['user-agent'] || '';
  if (!userAgentString) {
    return defaultInfo;
  }

  // 提取浏览器信息
  const browserInfo = parseBrowser(userAgentString);
  
  // 提取操作系统信息
  const osInfo = parseOS(userAgentString);
  
  // 判断是否为移动设备
  const isMobile = checkMobile(userAgentString);
  
  // 提取设备类型
  const device = parseDevice(userAgentString, isMobile);

  return {
    browser: browserInfo,
    os: osInfo,
    device,
    isMobile
  };
}

/**
 * 解析浏览器信息
 */
function parseBrowser(ua: string): { name: string; version: string } {
  // 常见浏览器识别
  if (ua.includes('Firefox/')) {
    const version = ua.match(/Firefox\/([\d.]+)/)?.[1] || '';
    return { name: 'Firefox', version };
  }
  
  if (ua.includes('Chrome/') && !ua.includes('Edg/') && !ua.includes('OPR/')) {
    const version = ua.match(/Chrome\/([\d.]+)/)?.[1] || '';
    return { name: 'Chrome', version };
  }
  
  if (ua.includes('Safari/') && !ua.includes('Chrome/') && !ua.includes('Edg/')) {
    const version = ua.match(/Version\/([\d.]+)/)?.[1] || '';
    return { name: 'Safari', version };
  }
  
  if (ua.includes('Edg/') || ua.includes('Edge/')) {
    const version = (ua.match(/Edg\/([\d.]+)/)?.[1] || ua.match(/Edge\/([\d.]+)/)?.[1] || '');
    return { name: 'Edge', version };
  }
  
  if (ua.includes('OPR/') || ua.includes('Opera/')) {
    const version = (ua.match(/OPR\/([\d.]+)/)?.[1] || ua.match(/Opera\/([\d.]+)/)?.[1] || '');
    return { name: 'Opera', version };
  }
  
  if (ua.includes('MSIE') || ua.includes('Trident/')) {
    const version = (ua.match(/MSIE ([\d.]+)/)?.[1] || ua.match(/rv:([\d.]+)/)?.[1] || '');
    return { name: 'Internet Explorer', version };
  }
  
  return { name: 'Unknown', version: '' };
}

/**
 * 解析操作系统信息
 */
function parseOS(ua: string): { name: string; version: string } {
  // Windows
  if (ua.includes('Windows')) {
    let version = 'Unknown';
    if (ua.includes('Windows NT 10.0')) version = '10';
    else if (ua.includes('Windows NT 6.3')) version = '8.1';
    else if (ua.includes('Windows NT 6.2')) version = '8';
    else if (ua.includes('Windows NT 6.1')) version = '7';
    else if (ua.includes('Windows NT 6.0')) version = 'Vista';
    else if (ua.includes('Windows NT 5.1')) version = 'XP';
    return { name: 'Windows', version };
  }
  
  // macOS
  if (ua.includes('Macintosh') || ua.includes('Mac OS X')) {
    const version = (ua.match(/Mac OS X (\d+[._]\d+)/)?.[1] || '').replace('_', '.');
    return { name: 'macOS', version };
  }
  
  // Linux
  if (ua.includes('Linux')) {
    const isAndroid = ua.includes('Android');
    if (isAndroid) {
      const version = ua.match(/Android ([\d.]+)/)?.[1] || '';
      return { name: 'Android', version };
    }
    return { name: 'Linux', version: '' };
  }
  
  // iOS
  if (ua.includes('iPhone') || ua.includes('iPad') || ua.includes('iPod')) {
    const version = (ua.match(/OS (\d+[._]\d+)/)?.[1] || '').replace('_', '.');
    return { name: 'iOS', version };
  }
  
  return { name: 'Unknown', version: '' };
}

/**
 * 检查是否为移动设备
 */
function checkMobile(ua: string): boolean {
  const mobileKeywords = [
    'Mobile', 'Android', 'iPhone', 'iPad', 'iPod', 'Windows Phone', 'BlackBerry', 'Opera Mini'
  ];
  
  return mobileKeywords.some(keyword => ua.includes(keyword));
}

/**
 * 解析设备类型
 */
function parseDevice(ua: string, isMobile: boolean): string {
  if (ua.includes('iPhone')) return 'iPhone';
  if (ua.includes('iPad')) return 'iPad';
  if (ua.includes('iPod')) return 'iPod';
  
  if (isMobile) {
    if (ua.includes('Android')) {
      if (ua.includes('Mobile')) return 'Android Phone';
      return 'Android Tablet';
    }
    return 'Mobile Device';
  }
  
  return 'Desktop';
}

export default {
  getUserAgent,
  setCurrentRequest
}; 